#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define MAX 100
#define THREADNUM 100

void *thread_func(void *arg)
{
  int i = *((int *)arg);
  int flag = 1;
  for (int j = 2; j <= i / 2; j++) {
    if (i % j == 0) {
      flag = 0;
      break;
    }
  }

  if (flag) {
    printf("%d\n", i);
  }

  pthread_exit(arg);
}

int main()
{
  pthread_t thread_ids[THREADNUM] = {0};
  int err;
  void *retvalue;
  for (int i = 2; i < MAX; i++) {
    int *ip = (int *)malloc(sizeof(int));
    *ip = i; 
    err = pthread_create(thread_ids + i, NULL, thread_func, ip);
    if (err) {
      fprintf(stderr, "pthread_create");
      exit(1);
    }
  }

  for (int i = 0; i < THREADNUM; i++) {
    if (thread_ids[i] != 0) {
      pthread_join(thread_ids[i], &retvalue);
      free(retvalue);
    }
  }
  exit(0);
}
